

#import "LLButtonCell.h"

@interface LLButtonCell ()
@property (strong, nonatomic) NSMutableArray *btns;
@property (strong, nonatomic) LLButton *selectedBtn;
@end

@implementation LLButtonCell

- (NSMutableArray *)btns {
    if (_btns == nil) {
        _btns = [NSMutableArray array];
    }
    return _btns;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self conDefaultSetting];
    }
    return self;
}

- (void)conDefaultSetting {
    self.leftMargin = 40;
    self.btnMargin = 20;
}

- (void)setBtnTitles:(NSArray *)btnTitles {
    _btnTitles = btnTitles;
    // 移除已经添加的btn
    for (UIView *subView in self.contentView.subviews) {
        if ([subView isKindOfClass:[LLButton class]]) {
            [subView removeFromSuperview];
        }
    }
    for (NSInteger i = 0; i < btnTitles.count; i++) {
        if (i < self.btns.count) {
            LLButton *btn = self.btns[i];
            [btn setTitle:btnTitles[i] forState:0];
            [self.contentView addSubview:btn];
        } else {
            LLButton *btn = [self createBtnWithTag:i];
            [btn setTitle:btnTitles[i] forState:0];
        }
    }
}

- (LLButton *)createBtnWithTag:(NSInteger)tag {
    _button = [LLButton new];
    _button.tag = tag;
    [_button addTarget:self action:@selector(buttonDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_button];
    [self.btns addObject:_button];
    return _button;
}

- (void)setBgColor:(NSArray *)bgColor {
    _bgColor = bgColor;
    for (NSInteger i = 0; i < bgColor.count; i++) {
        LLButton *btn = self.btns[i];
        [btn setBackgroundColor:bgColor[i]];
    }
}

- (void)setBgDisableColor:(NSArray *)bgDisableColor {
    _bgDisableColor = bgDisableColor;
    for (NSInteger i = 0; i < bgDisableColor.count; i++) {
        LLButton *btn = self.btns[i];
        [btn setBackgroundColor:bgDisableColor[i]];
    }
}

- (void)setBgImage:(NSArray *)bgImage {
    _bgImage = bgImage;
    for (NSInteger i = 0; i < bgImage.count; i++) {
        LLButton *btn = self.btns[i];
        [btn setBackgroundImage:[UIImage imageNamed:bgImage[i]] forState:0];
    }
}

- (void)setNormalLmage:(NSArray *)normalLmage {
    _normalLmage = normalLmage;
    for (NSInteger i = 0; i < normalLmage.count; i++) {
        LLButton *btn = self.btns[i];
        [btn setImage:[UIImage imageNamed:normalLmage[i]] forState:0];
    }
}

- (void)setSelectedLmage:(NSArray *)selectedLmage {
    _selectedLmage = selectedLmage;
    for (NSInteger i = 0; i < selectedLmage.count; i++) {
        LLButton *btn = self.btns[i];
        [btn setImage:[UIImage imageNamed:selectedLmage[i]] forState:UIControlStateSelected];
    }
}

- (void)setTextColor:(NSArray *)textColor {
    _textColor = textColor;
    for (NSInteger i = 0; i < textColor.count; i++) {
        LLButton *btn = self.btns[i];
        [btn setTitleColor:textColor[i] forState:0];
    }
}

- (void)setDisableTextColor:(NSArray *)disableTextColor {
    _disableTextColor = disableTextColor;
    for (NSInteger i = 0; i < disableTextColor.count; i++) {
        LLButton *btn = self.btns[i];
        [btn setTitleColor:disableTextColor[i] forState:UIControlStateDisabled];
    }
}

- (void)setShowDisableState:(NSArray *)showDisableState {
    _showDisableState = showDisableState;
    for (NSInteger i = 0; i < showDisableState.count; i++) {
        LLButton *btn = self.btns[i];
        NSNumber *num = showDisableState[i];
        btn.enabled = num.integerValue == 1;
        if (num.integerValue == 1) {
            for (NSInteger i = 0; i < self.bgColor.count; i++) {
                [btn setBackgroundColor:self.bgColor[i]];
            }
        } else {
            for (NSInteger i = 0; i < self.bgDisableColor.count; i++) {
                [btn setBackgroundColor:self.bgDisableColor[i]];
            }
        }
    }
}

- (void)setBtnFont:(NSArray *)btnFont {
    _btnFont = btnFont;
    for (NSInteger i = 0; i < btnFont.count; i++) {
        LLButton *btn = self.btns[i];
        NSInteger font = [btnFont[i] integerValue];
         btn.titleLabel.font = LLRegularFont(font);
    }
}

- (void)buttonDidClick:(LLButton *)btn {
    if ([self.delegate respondsToSelector:@selector(buttonCelldidClickButton:)]) {
        [self.delegate buttonCelldidClickButton:btn];
    }
}

#pragma mark --- layoutSubviews

- (void)layoutSubviews {
    [super layoutSubviews];
    NSInteger count = self.btnTitles.count;
    CGFloat btnW = (self.width - self.leftMargin*2 - (count - 1)*self.btnMargin)/count;
    CGFloat btnX = 0;
    CGFloat btnY = self.btnTop ? self.btnTop : 0;
    CGFloat btnH = self.height - 2 * btnY;
    for (NSInteger i = 0; i < count; i++) {
        LLButton *btn = self.btns[i];
        btn.layer.cornerRadius = self.btnCornerRadius ? self.btnCornerRadius : btnH / 2.5;
        btn.clipsToBounds = YES;
        btnX = self.leftMargin + (btnW + self.btnMargin)*i;
        btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
    }
}

@end
